﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BaseSet.Data;
using BaseSet.Logic;
using BaseSet.Entity;
using BaseSet.Framework;
using BaseSet.Entity.Common;
using BaseSet.Entity.Common.Text;
using BaseSet;
using System.Data;

namespace WebSite.UserControl
{
    public partial class Head : System.Web.UI.UserControl
    {
        public string serverpath = System.Configuration.ConfigurationManager.AppSettings["ServerPath"] + "";
        //public string currentpageurl = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                //当前页面URL
                //currentpageurl = Request.Url.AbsoluteUri;
                Users uinfo = UserBase.GetCurrentUser();
                if (uinfo == null)//没有登录
                {
                    //读取cookie中的值
                    // 请求上次存储的Cookies
                    HttpCookie cookies = Request.Cookies["USER_COOKIE"];
                    // 如果此Cookies存在且它里面有子键则进行读取

                    if (cookies != null && cookies.HasKeys)
                    {
                        string username = cookies["username"];
                        string password = cookies["password"];//此密码为加密密码

                        UsersHelper uhelper = new UsersHelper();
                        Criteria c = new Criteria();
                        c.Add(CriteriaType.Equals, "UserName", username);
                        c.Add(CriteriaType.Equals, "Password", password);
                        c.Add(CriteriaType.Equals, "RoleType", 1);
                        c.Add(CriteriaType.Equals, "Flag", 0);
                        List<Users> userlist = uhelper.QueryUsersBySearch(c, null);

                        if (userlist != null && userlist.Count > 0)
                        {
                            Users user = userlist[0];
                            System.Web.HttpContext.Current.Session["CurrentUser"] = user;

                            //更新此用户的最后登录日期
                            user.LastAccessDate = System.DateTime.Now;
                            uhelper.EditUsers(user);

                            litNickName.Text = user.NickName;
                            //级别
                            string roleid = user.RoleID;
                            Role rinfo = new Role();
                            RoleHelper rhelper = new RoleHelper();
                            rinfo = rhelper.GetRole(roleid);
                            lblRoleName.Text = rinfo.RoleName;
                            login.Visible = false;
                            loggedin.Visible = true;
                        }
                        else
                        {
                            login.Visible = true;
                            loggedin.Visible = false;
                        }
                    }
                    else
                    {
                        login.Visible = true;
                        loggedin.Visible = false;
                    }   
                }
                else//已经登录
                {
                    litNickName.Text = uinfo.NickName;
                    //级别
                    string roleid = uinfo.RoleID;
                    Role rinfo = new Role();
                    RoleHelper rhelper = new RoleHelper();
                    rinfo = rhelper.GetRole(roleid);
                    lblRoleName.Text = rinfo.RoleName;
                    login.Visible = false;
                    loggedin.Visible = true;
                    //加载未读消息
                    MessageHelper mhelper = new MessageHelper();
                    Criteria c = new Criteria();
                    c.Add(CriteriaType.Equals, "ToUsersID", uinfo.ID);
                    c.Add(CriteriaType.Equals, "Flag", 0);
                    c.Add(CriteriaType.Equals, "Status", 0);
                    int count = mhelper.QueryMessageCountBySearch(c);
                    lblMessageCount.Text = count.ToString();
                }
            }
        }

        #region 退出
        protected void lbLogOut_Click(object sender, EventArgs e)
        {
            //清空cookie
            Response.Cookies["USER_COOKIE"].Expires = DateTime.Now.AddDays(-1); 

            System.Web.HttpContext.Current.Session["CurrentUser"] = null;
            //string serverpath = System.Configuration.ConfigurationManager.AppSettings["ServerPath"] + "";
            Response.Redirect(serverpath+"/Index.aspx");
        }
        #endregion
    }
}